package com.lanyou.esb.servicemix.util;


import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.cxf.jaxrs.client.WebClient;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.ParamSetting;
import com.lanyou.cook.paramsetting.ParamSettingReppository;
import com.lanyou.cook.service.ApplicationContextKeeper;
import com.lanyou.cook.util.gson.GsonUtil;
import com.lanyou.esb.service.SettingPortService;

public class FtpRouteHandleUtil {

	/**
	 * 请求ftp路由的工具类
	 * @return
	 */
	public static boolean process(String methodName, String camelId){
		boolean status = false;
		ParamSettingReppository paramSettingReppository = ApplicationContextKeeper.getApplicationContext().getBean(ParamSettingReppository.class);
		ParamSetting paramSettingServer = paramSettingReppository.findByParamName(Constants.SERVERMIX_SERVER);
		//TODO
		if(paramSettingServer==null){
			status =  false;
			return status;
		}
		JsonArray jsonDatas = GsonUtil.getDefaultJsonParser()
				.parse(paramSettingServer.getParamValue()).getAsJsonArray();
		for (JsonElement jsonElement : jsonDatas) {
			String strIp = jsonElement.getAsJsonObject().get("ip").getAsString();
			String strPort = jsonElement.getAsJsonObject().get("port").getAsString();
		    String requestAddress = "http://" + strIp + ":" + strPort + "/" + Constants.ROUTE_REQUEST_ADDRESS;
	        /*Response reponse = WebClient.create(Constants.FTP_ROUTE_REQUEST_ADDRESS).query("methodName", methodName).query("camelId", camelId).get();*/
		    try{
			    Response reponse = WebClient.create(requestAddress).query("methodName", methodName).query("camelId", camelId).get();
			    if(reponse.getStatus() == 204){
		        	status =  true;
		        }else{
		        	status =  false;
		        	return status;
		        }
		    }catch(Exception e){
		    	e.printStackTrace();
		    	return false;
		    }
		}
		return status;
	}
	
	public static String getLocalHost(){
		/*ParamSettingReppository paramSettingReppository = ApplicationContextKeeper.getApplicationContext().getBean(ParamSettingReppository.class);
		ParamSetting paramSettingServer = paramSettingReppository.findByParamName(Constants.FTP_EXTERNAL_REQUEST_IP);
		//TODO
		if(paramSettingServer != null){
			return paramSettingServer.getParamValue();
		}else{
			return "";
		}*/
		int port = SettingPortService.getPort();
		String os = System.getProperty("os.name");  
		if(os.toLowerCase().startsWith("win")){ 
			//windows
			return getWindowsIP() + ":" + port;
		}else{
			//linux
			return getLinuxIP() + ":" + port;
		}
		
		
	}
	
	
	public static String getWindowsIP(){
		try {
			return InetAddress.getLocalHost().getCanonicalHostName();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		} 
	}
	
	public static String getLinuxIP() {
        String ip = "";
        try {
            Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();
            while (e1.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) e1.nextElement();
                if (!ni.getName().contains("eth")) {
                    continue;
                } else {
                    Enumeration<?> e2 = ni.getInetAddresses();
                    while (e2.hasMoreElements()) {
                        InetAddress ia = (InetAddress) e2.nextElement();
                        if (ia instanceof Inet6Address)
                            continue;
                        ip = ia.getCanonicalHostName();
                    }
                    break;
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
            System.exit(-1);
        }
        return ip;
    }
	
	public static String getLinuxIp(){
		Enumeration<NetworkInterface> allNetInterfaces;  //定义网络接口枚举类  
        try {  
            allNetInterfaces = NetworkInterface.getNetworkInterfaces();  //获得网络接口  
  
            InetAddress ip = null; //声明一个InetAddress类型ip地址  
            while (allNetInterfaces.hasMoreElements()) //遍历所有的网络接口  
            {  
                NetworkInterface netInterface = allNetInterfaces.nextElement();  
                System.out.println(netInterface.getName());  //打印网端名字  
                Enumeration<InetAddress> addresses = netInterface.getInetAddresses(); //同样再定义网络地址枚举类  
                while (addresses.hasMoreElements())  
                {  
                    ip = addresses.nextElement();  
                    if (ip != null && (ip instanceof Inet4Address)) //InetAddress类包括Inet4Address和Inet6Address  
                    {  
                        System.out.println("本机的IP = " + ip.getHostAddress());  
                        return ip.getHostAddress();
                    }else{
                    	return "";
                    }
                }  
            } 
            
            return "";
        } catch (SocketException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();
            return "";
        }  
	}
	
	
	/*public static void main(String[] args) {
			
	     String methodName = "restartCamel";
	     String camelId = "test";
	     System.out.println(process(methodName, camelId));
           
	}*/
 
}
